/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2011 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id: ProcessorAlarmTriggerTestCase.java 444 2011-08-16 16:24:00Z guillaume.sauthier $
 * --------------------------------------------------------------------------
 */


package org.osami.gateway.mediator.alarm.internal;

import java.util.Collections;
import java.util.List;

import fr.liglab.adele.cilia.Data;
import org.hamcrest.Matchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.osami.gateway.alarm.IAlarm;
import org.osami.gateway.profile.IProfileService;
import org.osgi.service.log.LogService;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.startsWith;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

/**
 * A {@code ProcessorAlarmTriggerTestCase} is ...
 *
 * @author Guillaume Sauthier
 */
public class ProcessorAlarmTriggerTestCase {

    @Mock
    private LogService logService;

    @Mock
    private IProfileService heatingProfile;

    @Mock
    private IProfileService luminosityProfile;

    private ProcessorAlarmTrigger trigger;

    @BeforeMethod
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        // Bind the trigger
        trigger = new ProcessorAlarmTrigger();
        trigger.setHeatingProfile(heatingProfile);
        trigger.setLuminosityProfile(luminosityProfile);
        trigger.setLogService(logService);

        // Default behaviors
        when(luminosityProfile.getParameter(eq("topValue"))).thenReturn("100");
        when(luminosityProfile.getParameter(eq("bottomValue"))).thenReturn("50");
        when(luminosityProfile.getParameter(eq("plugId"))).thenReturn("0:1:2:3:4");

        when(heatingProfile.getParameter(eq("targetTemperature"))).thenReturn("23");
        when(heatingProfile.getParameter(eq("targetDelta"))).thenReturn("3");
    }

    @Test
    public void testThatAverageLuminosityAboveGivenThresholdTriggersAnAlarm() throws Exception {
        Data input = new Data(null);
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_LOCATION_KEY, "my-office");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_TYPE_KEY, "light");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_VALUE_KEY, 110d);

        List<Data> result = trigger.process(Collections.singletonList(input));
        Data alarm = result.get(0);

        // Test mandatory attributes (except timestamp since we don't have a Clock abstraction)
        assertThat(alarm, is(notNullValue()));
        assertThat((String) alarm.getProperty(IAlarm.ID_PROPERTY), startsWith("LUM-ABV-"));
        assertThat((String) alarm.getProperty(IAlarm.SEVERITY_PROPERTY), equalTo("INFO"));
        assertThat(alarm.getProperty(IAlarm.MESSAGE_PROPERTY), is(notNullValue()));

        verifyZeroInteractions(heatingProfile);
        verifyZeroInteractions(logService);
    }

    @Test
    public void testThatAverageLuminosityBelowGivenThresholdTriggersAnAlarm() throws Exception {

        Data input = new Data(null);
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_LOCATION_KEY, "my-office");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_TYPE_KEY, "light");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_VALUE_KEY, 25d);

        List<Data> result = trigger.process(Collections.singletonList(input));
        Data alarm = result.get(0);

        // Test mandatory attributes (except timestamp since we don't have a Clock abstraction)
        assertThat(alarm, is(notNullValue()));
        assertThat((String) alarm.getProperty(IAlarm.ID_PROPERTY), startsWith("LUM-BLW-"));
        assertThat((String) alarm.getProperty(IAlarm.SEVERITY_PROPERTY), equalTo("INFO"));
        assertThat(alarm.getProperty(IAlarm.MESSAGE_PROPERTY), is(notNullValue()));

        verifyZeroInteractions(heatingProfile);
        verifyZeroInteractions(logService);
    }

    @Test
    public void testThatAverageTemperatureAboveGivenThresholdTriggersAnAlarm() throws Exception {
        Data input = new Data(null);
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_LOCATION_KEY, "my-office");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_TYPE_KEY, "temperature");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_VALUE_KEY, 30d);

        List<Data> result = trigger.process(Collections.singletonList(input));
        Data alarm = result.get(0);

        // Test mandatory attributes (except timestamp since we don't have a Clock abstraction)
        assertThat(alarm, is(notNullValue()));
        assertThat((String) alarm.getProperty(IAlarm.ID_PROPERTY), startsWith("HEAT-OVR-"));
        assertThat((String) alarm.getProperty(IAlarm.SEVERITY_PROPERTY), equalTo("WARNING"));
        assertThat(alarm.getProperty(IAlarm.MESSAGE_PROPERTY), is(notNullValue()));

        verifyZeroInteractions(luminosityProfile);
        verifyZeroInteractions(logService);
    }

    @Test
    public void testThatAverageTemperatureBelowGivenThresholdTriggersAnAlarm() throws Exception {
        Data input = new Data(null);
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_LOCATION_KEY, "my-office");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_TYPE_KEY, "temperature");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_VALUE_KEY, 12d);

        List<Data> result = trigger.process(Collections.singletonList(input));
        Data alarm = result.get(0);

        // Test mandatory attributes (except timestamp since we don't have a Clock abstraction)
        assertThat(alarm, is(notNullValue()));
        assertThat((String) alarm.getProperty(IAlarm.ID_PROPERTY), startsWith("HEAT-BLW-"));
        assertThat((String) alarm.getProperty(IAlarm.SEVERITY_PROPERTY), equalTo("WARNING"));
        assertThat(alarm.getProperty(IAlarm.MESSAGE_PROPERTY), is(notNullValue()));

        verifyZeroInteractions(luminosityProfile);
        verifyZeroInteractions(logService);
    }

    @Test
    public void testThatUnknownTypeIsIgnored() throws Exception {
        Data input = new Data(null);
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_LOCATION_KEY, "my-office");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_TYPE_KEY, "undefined");
        input.setProperty(ProcessorAlarmTrigger.AVERAGE_VALUE_KEY, 1226d);

        List<Data> result = trigger.process(Collections.singletonList(input));

        assertThat(result.size(), is( equalTo( 0 ) ));

        verify(logService).log(eq(LogService.LOG_WARNING), anyString());

        verifyZeroInteractions(luminosityProfile);
        verifyZeroInteractions(heatingProfile);

    }

}
